package com.fage.nio.demo;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author lin
 * @version 1.0
 * @date 2020-08-11 16:52
 * @Description TODO
 */
public class IoNioMain {
    private static String filePath = "D:\\零碎\\Spring\\Spring 事务.md";

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        io();
//        nio();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        int a = 12;
        char b = 'a';
        int c = a + b;
        System.out.println('a');
        System.out.println(b);
        System.out.println((int)b);
    }

    public static void io() throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(filePath));
        //读取数据
        //一次性取多少字节
        byte[] bytes = new byte[2048];
        //接受读取的内容(n就代表的相关数据，只不过是数字的形式)
        int n = -1;
        while ((n = is.read(bytes, 0, bytes.length)) != -1) {
            //转换成字符串
            String str = new String(bytes, 0, n);
//            String str = new String(bytes,0,n,"GBK");
            System.out.println(str);
        }
        //关闭流
        is.close();
    }

    public static void nio() throws IOException {
//        RandomAccessFile file = new RandomAccessFile(filePath, "rw");
//        FileChannel channel = file.getChannel();
        List<String> lists = Files.readAllLines(Paths.get(filePath));
        int count = 0;
        for (String str : lists) {
            // 输出文件内容
            System.out.println(str);
            count++;
        }
        System.out.println("总共输出: " + count + " 条");

    }

}
